﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Xml;
using System.Diagnostics;

namespace CPAScoringPerformanceTester.Models
{
    class Preference : INotifyPropertyChanged
    {
        #region Fields
        private const string FILE_USER_PREFERENCE = "user-preference.xml";
        private const string OPTION_ROBOT_CONTAINER_WIDTH = "robot-container-display/width";
        private const string OPTION_ROBOT_CONTAINER_HEIGHT = "robot-container-display/height";
        public static Preference Global = new Preference();
        public event PropertyChangedEventHandler PropertyChanged;
        #endregion

        #region Properties
        private int robotContainerWidth;
        public int RobotContainerWidth
        {
            get { return robotContainerWidth; }
            set
            {
                robotContainerWidth = value;
            }
        }

        private int robotContainerHeight;
        public int RobotContainerHeight
        {
            get { return robotContainerHeight; }
            set
            {
                robotContainerHeight = value;
            }
        }
        #endregion

        #region Construction
        private Preference()
        { }
        #endregion

        #region Public Methods
        public bool Load()
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(FILE_USER_PREFERENCE);

                RobotContainerWidth = Convert.ToInt32(GetOptionValue(doc, OPTION_ROBOT_CONTAINER_WIDTH));
                RobotContainerHeight = Convert.ToInt32(GetOptionValue(doc, OPTION_ROBOT_CONTAINER_HEIGHT));

                return true;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                return false;
            }
        }

        public void Apply()
        { 
            OnPropertyChanged("RobotContainerWidth");
            OnPropertyChanged("RobotContainerHeight");
        }

        public bool SetDefault()
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(FILE_USER_PREFERENCE);

                SetOptionValue(doc, OPTION_ROBOT_CONTAINER_WIDTH, RobotContainerWidth);
                SetOptionValue(doc, OPTION_ROBOT_CONTAINER_HEIGHT, RobotContainerHeight);

                doc.Save(FILE_USER_PREFERENCE);

                return true;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                return false;
            }
        }
        #endregion

        #region Private Methods
        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        private string GetOptionValue(XmlDocument doc, string name)
        {
            XmlNode node = doc.SelectSingleNode(string.Format("/preference/{0}", name));
            return node.InnerText;
        }

        private void SetOptionValue(XmlDocument doc, string name, object value)
        {
            XmlNode node = doc.SelectSingleNode(string.Format("/preference/{0}", name));
            node.InnerText = value.ToString();
        }
        #endregion
    }
}
